package com.chencs.app.lianmeng.service;

import com.chencs.app.AppCommon;
import com.chencs.app.lianmeng.mapper.LMCapitalMapper;
import com.chencs.app.lianmeng.mapper.LMMemberMapper;
import com.chencs.vo.biz.lianmeng.LMAccount;
import com.chencs.vo.biz.lianmeng.LmPay_account;
import com.chencs.vo.biz.lianmeng.LmWithdraw;
import com.chencs.vo.net.lmrequest.*;
import com.chencs.vo.net.lmresponse.LmPayAccountResponse;
import com.chencs.vo.net.lmresponse.LmStatisticsResponse;
import com.chencs.vo.net.lmresponse.LmSummaryResponse;
import com.chencs.vo.net.lmresponse.LmWithdrawResponse;
import com.chencs.vo.net.response.ResponseBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class LMCapitalSer {


    @Autowired
    private LMCapitalMapper capitalMapper;
    @Autowired
    private LMMemberMapper memberMapper;

    /**
     * 发起提现
     * @param request
     * @return
     */
    @Transactional
    public ResponseBase createWithdraw(LmWithdrawRequest request){
        ResponseBase response = new ResponseBase();
        //减去余额
        //冻结金额增加
        //添加提现记录
        //修改账户

        LMAccount account = memberMapper.getLmAccount(request.getUid());
        if(account!=null){
            if(request.getAmount()>=0&&(account.getBalance()-request.getAmount()<0)){
                response.setCode(-1008);
                response.setMsg("账户余额不足!");
                return  response;
            }

            LmWithdraw lmWithdraw = new LmWithdraw();
            lmWithdraw.setAccount(request.getAccount());
            lmWithdraw.setName(request.getName());
            lmWithdraw.setAmount(request.getAmount());
            lmWithdraw.setUid(request.getUid());
            lmWithdraw.setType(0);
            capitalMapper.createWithdraw(lmWithdraw);
            account.setBalance(account.getBalance()-request.getAmount());
            account.setFreeze(account.getFreeze()+request.getAmount());
            account.setAccumulate(account.getAccumulate());
            account.setLastmonth(account.getLastmonth());
            memberMapper.updateAccount(account);
            response.setCode(AppCommon.SUCCESS);
            response.setMsg("申请成功!");
            return  response;
        }else{
            response.setCode(-1001);
            response.setMsg("账户余异常!");
            return  response;
        }

    }

    /**
     * 获取提现记录
     * @param request
     * @return
     */
    public LmWithdrawResponse WithdrawList(LmWithdrawRequest request){
        LmWithdrawResponse response = new LmWithdrawResponse();
        Map<String,Object> map = new HashMap<>();
        map.put("uid",request.getUid());
        map.put("pageno",request.getPageNo()*request.getPageSize());
        map.put("pagesize",request.getPageSize());
        List<LmWithdraw> lmWithdrawList = capitalMapper.getWithdrawList(map);
        if(CollectionUtils.isEmpty(lmWithdrawList)){
            response.setCode(AppCommon.NULL);
            response.setMsg(AppCommon.NULLMSG);
        }else{
            response.setList(lmWithdrawList);
            response.setMsg(AppCommon.SUCCESSMSG);
            response.setCode(AppCommon.SUCCESS);
        }
        return response;
    }

    /**
     * 取支付宝帐号
     * @param request
     * @return
     */
    public LmPayAccountResponse getPayAccount(LmPayAccountRequest request){
        LmPayAccountResponse response = new LmPayAccountResponse();
        LmPay_account pay_account = capitalMapper.getPayAccount(request.getUid());
        if(pay_account==null){
            response.setCode(AppCommon.NULL);
            response.setMsg(AppCommon.NULLMSG);
        }else{
            response.setLmPay_account(pay_account);
            response.setMsg(AppCommon.SUCCESSMSG);
            response.setCode(AppCommon.SUCCESS);
        }

        return  response;
    }

    /**
     * 添加支付宝
     */
    public ResponseBase createPayAccount(LmPayAccountEditorRequest request){
        ResponseBase response = new ResponseBase();
        try {
            LmPay_account pay_account = new LmPay_account();
            pay_account.setAccount(request.getAccount());
            pay_account.setUid(request.getUid());
            pay_account.setReal_name(request.getReal_name());
            pay_account.setFlag("T");
            capitalMapper.createPayAccount(pay_account);
            response.setMsg(AppCommon.SUCCESSMSG);
            response.setCode(AppCommon.SUCCESS);
        }catch (Exception e){
            response.setMsg(AppCommon.ERRORMSG);
            response.setCode(AppCommon.ERROR);
        }
        return  response;
    }


    /**
     * 修改支付宝
     */
    public ResponseBase editorPayAccount(LmPayAccountEditorRequest request){
        ResponseBase response = new ResponseBase();
        try {
            LmPay_account pay_account = new LmPay_account();
            pay_account.setAccount(request.getAccount());
            pay_account.setReal_name(request.getReal_name());
            pay_account.setId(request.getId());
            capitalMapper.editorPayAccount(pay_account);
            response.setMsg("操作成功");
            response.setCode(AppCommon.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            response.setMsg(AppCommon.ERRORMSG);
            response.setCode(AppCommon.ERROR);
        }
        return  response;
    }


    /**
     * 收入
     */












    /**
     * 整体概括
     */
    public LmStatisticsResponse getStatistics(LmStatisticsRequest request){
        LmStatisticsResponse response = new LmStatisticsResponse();

        return  response;
    }

    /**
     * 整体概括详情
     */
    public LmSummaryResponse getSummary(LmSummaryRequest request){
        LmSummaryResponse response = new LmSummaryResponse();

        return  response;
    }





}
